package com.hwua.dao.impl;

import com.hwua.dao.UserDao;
import com.hwua.entity.User;
import com.hwua.util.DBHelper;

import javax.jws.soap.SOAPBinding;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CQY
 */
public class UserDaoImpl implements UserDao{
    @Override
    public List<User> findAll() {
        Connection conn=null;
        String sql="select * from tb_adminRole";
        PreparedStatement pre=null;
        ResultSet rs=null;
        List<User> list=null;
        User user=null;
        try {
           conn= DBHelper.getConnection();
           pre =conn.prepareStatement(sql);
            rs=pre.executeQuery();
            list=new ArrayList<User>();
            while (rs.next()){
                user=new User();
                user.setAdmin_id(rs.getInt(1));
                user.setAdmin_name(rs.getString(2));
                user.setAdmin_pwd(rs.getString(3));
                user.setAdmin_email(rs.getString(4));
                user.setAdmin_phone(rs.getString(5));
                user.setRole(rs.getString(6));
                list.add(user);
            }
            rs.close();
            pre.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public User findByName(String name) {
        Connection conn=null;
        String sql="select * from tb_adminRole where admin_name=?";
        PreparedStatement pre=null;
        ResultSet rs=null;
        List<User> list=null;
        User user=null;
        try {
            conn= DBHelper.getConnection();
            pre =conn.prepareStatement(sql);
            pre.setString(1,name);
            rs=pre.executeQuery();
            if (rs.next()){
                user=new User();
                user.setAdmin_id(rs.getInt(1));
                user.setAdmin_name(rs.getString(2));
                user.setAdmin_pwd(rs.getString(3));
                user.setAdmin_email(rs.getString(4));
                user.setAdmin_phone(rs.getString(5));
                user.setRole(rs.getString(6));
            }
            rs.close();
            pre.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User findById(int id) {
        return null;
    }

    @Override
    public int save(User user) {
        String sql="insert into tb_adminRole(admin_id,admin_name,admin_pwd,admin_email,admin_phone,role)" +
                "  values(?,?,?,?,?,?)";
        Object[] params={user.getAdmin_id(),user.getAdmin_name(),user.getAdmin_pwd(),user.getAdmin_email(),user.getAdmin_phone(),user.getRole()};
        int row=0;
        try {
            row=DBHelper.update(sql,params);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int delete(int id) {
        String sql="delete from tb_adminRole where admin_id=?";
        Object[] params={id};
        int row=0;
        try {
            row=DBHelper.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int update(User user) {
        String sql="update tb_adminRole set admin_name=?,admin_pwd=?,admin_email=?,admin_phone=?,role=? where admin_id=?";
        Object[] params={user.getAdmin_name(),user.getAdmin_pwd(),user.getAdmin_email(),user.getAdmin_phone(),user.getRole(),user.getAdmin_id()};
        int row=0;
        try {
            row=DBHelper.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
}
